/*
 * @lc app=leetcode.cn id=443 lang=javascript
 *
 * [443] 压缩字符串
 */

// @lc code=start
/**
 * @param {character[]} chars
 * @return {number}
 */
var compress = function (chars) {
    let len = chars.length
    let count = 1
    for (let i = len - 1; i > 0; i--) {
        if (chars[i] == chars[i - 1]) {
            count++
        } else {
           if(count!=1){ chars.splice(i + 1, count - 1, count); count = 1}
        }
        
    }
    if(count!=1)chars.splice(1,count-1,count)
    len=chars.length
    for(let i=len-1;i>0;i--){
        if(chars[i]>=10){
            chars.splice(i,1,...(chars[i].toString().split("")))
        }
        chars[i]=chars[i]+""
    }
    return chars.length
};
// @lc code=end

var compress = function (chars) {
    var len = chars.length
    for (var i = 0, j = 0; j < len;) {
      chars[i] = chars[j]
      var temp = j
      while (j < len && chars[i] === chars[j]) {
        j++
      }
      i++
      var dis = j - temp
      if (dis > 1) {
        var distance = Array.from('' + dis)
        for (var k = 0; k < distance.length; k++) {
          chars[i++] = distance[k]
        }
      }
  
    }
    return i
  }//双指针法